package com.thinta.wind.ui.home.fragment

import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.SimpleItemAnimator
import com.thinta.wind.JetConstant
import com.thinta.wind.R
import com.thinta.wind.base.BaseFragment
import com.thinta.wind.databinding.NearByFragmentBinding
import com.thinta.wind.entity.User
import com.thinta.wind.interfaces.OnItemClickListener
import com.thinta.wind.ui.home.adapter.HotComAdapter
import com.thinta.wind.ui.home.repository.HotComFactory
import com.thinta.wind.ui.home.repository.HotComRepository
import com.thinta.wind.ui.home.viewmodel.HotComViewModel
import com.thinta.wind.utils.PreferenceUtils

class NearByFragment : BaseFragment<HotComViewModel, NearByFragmentBinding>() {

    companion object {
        fun newInstance() = NearByFragment()
    }

    private val mHotComAdapter : HotComAdapter by lazy { HotComAdapter() }

    override fun getModelFactory(): ViewModelProvider.NewInstanceFactory = HotComFactory(
        HotComRepository()
    )

    override fun viewModelClass(): Class<HotComViewModel> = HotComViewModel::class.java

    override fun getLayoutId(): Int = R.layout.near_by_fragment

    override fun afterCreate() {
        dataBinding?.nearList?.layoutManager = LinearLayoutManager(context)
        dataBinding?.nearList?.isNestedScrollingEnabled = true
        (dataBinding?.nearList?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false
        dataBinding?.nearList?.adapter = mHotComAdapter
        val divider =  DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
        context?.let {
            ContextCompat.getDrawable(it, R.drawable.divider_selector)?.let { it1 ->
                divider.setDrawable(
                    it1
                )
            }
        }
        dataBinding?.nearList?.addItemDecoration(divider)
        initData()
        initListener()
    }

    private fun initData(){
        val user = PreferenceUtils.getObject(PreferenceUtils.USER_INFO, User::class.java) as User
        viewModel?.getHotData(JetConstant.HOT_NEAR_BY, JetConstant.PAGE_SIZE, user.id)
        viewModel?.result?.observe(this, {
            mHotComAdapter.submitData(lifecycle, it)
        })
    }

    private fun initListener(){
        mHotComAdapter.setOnItemListener(OnItemClickListener{ position: Int, view: View? ->

        })
    }
}

